package com.sxz.sbs.module.staff.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sxz.sbs.R;
import com.sxz.sbs.app.GlideApp;
import com.sxz.sbs.module.staff.mode.StaffDisposeBean;

import java.util.List;

/**
 * 选择员工 单选
 */

public class ChoseStaffAdapter extends BaseQuickAdapter<StaffDisposeBean.ListBean,ChoseStaffAdapter.ChoseStaffHolder>{
    private int mSelectedPos=-1;
    public ChoseStaffAdapter(@Nullable List<StaffDisposeBean.ListBean> data) {
        super(R.layout.item_chose_staff_layout,data);
    }

    @Override
    protected void convert(final ChoseStaffHolder helper, StaffDisposeBean.ListBean item) {
        GlideApp.with(mContext).load(item.getUrl_HeadPhoto()).placeholder(R.drawable.ic_headportrait)
                .error(R.drawable.ic_headportrait).into((ImageView) helper.getView(R.id.tiv_chose_staff_head));
        helper.setText(R.id.tv_chose_staff_name,item.getTxt_Name());
        helper.setText(R.id.tv_chose_staff_phone,item.getTxt_Phone());
        if (helper.getAdapterPosition() == mSelectedPos) {
            helper.checkBox.setChecked(true);
        } else {
            helper.checkBox.setChecked(false);
        }
        helper.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!helper.checkBox.isChecked()) {
                    helper.checkBox.setChecked(true);
                    setPressIndex(helper.getAdapterPosition());
                } else {
                    helper.checkBox.setChecked(false);
                    mSelectedPos = -1;
                }

            }
        });
    }
    public void setPressIndex (int index) {
       this.mSelectedPos = index;
       notifyDataSetChanged();
    }

    /**
     * 返回选择位置
     * @return
     */
    public int getSelectIndex () {
        return  mSelectedPos;
    }
    public class ChoseStaffHolder extends BaseViewHolder{
        CheckBox checkBox;
        public ChoseStaffHolder(View view) {
            super(view);
            checkBox = view.findViewById(R.id.cb_chose_staff);
        }
    }
}
